package com.socialrss.viewlisteners;

import com.socialrss.R;
import com.socialrss.utils.twitter.*;

import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;

import com.socialrss.utils.Util;

import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TwitterLoginActivity extends BaseActivity {

	// Login button
	Button btnLoginTwitter;
	// Update status button
	Button btnUpdateStatus;
	// Logout button
	Button btnLogoutTwitter;
	// EditText for update
	EditText txtUpdate;
	// lbl update
	TextView lblUpdate;
	
	String username = "";
	Uri uri;
	
	public static TextView lblUserName;

	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_twitter_login);
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	
		// Check if Internet present
		if (!Util.isConnectingToInternet(this)) {
			// Internet Connection is not present
			Util.showAlertDialog(this, "Internet Connection Error",
					"Please connect to working Internet connection", false);
			// stop executing code by return
			return;
		}
		// Check if twitter keys are set
		if(TwitterUtil.isKeysNotSet()){
			// Internet Connection is not present
			Util.showAlertDialog(this, "Twitter oAuth tokens", "Please set your twitter oauth tokens first!", false);
			// stop executing code by return
			return;
		}
		// All UI elements
		btnLoginTwitter = (Button) findViewById(R.id.btnLoginTwitter);
		btnUpdateStatus = (Button) findViewById(R.id.btnUpdateStatus);
		btnLogoutTwitter = (Button) findViewById(R.id.btnLogoutTwitter);
		txtUpdate = (EditText) findViewById(R.id.txtUpdateStatus);
		lblUpdate = (TextView) findViewById(R.id.lblUpdate);
		lblUserName = (TextView) findViewById(R.id.lblUserName);
				
		//hide the buttons
		btnLogoutTwitter.setVisibility(View.GONE);
		btnUpdateStatus.setVisibility(View.GONE);
		txtUpdate.setVisibility(View.GONE);
		lblUpdate.setVisibility(View.GONE);
		
		/**
		 * Twitter login button click event will call loginToTwitter() function
		 * */
		btnLoginTwitter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Call login twitter function
				TwitterUtil.loginToTwitter(TwitterLoginActivity.this);
			}
		});
		
		/**
		 * Button click event to Update Status, will call updateTwitterStatus()
		 * function
		 * */
		btnUpdateStatus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// Call update status function
				// Get the status from EditText
				String status = txtUpdate.getText().toString();

				// Check for blank text
				if (status.trim().length() > 0) {
					// update status
					new TwitterFriendsThread().execute();
				} else {
					// EditText is empty
					Toast.makeText(getApplicationContext(),
							"Please enter status message", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

		/**
		 * Button click event for logout from twitter
		 * */
		btnLogoutTwitter.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Call logout twitter function
				TwitterUtil.logoutFromTwitter();
				// After this take the appropriate action
				// I am showing the hiding/showing buttons again
				// You might not needed this code
				btnLogoutTwitter.setVisibility(View.GONE);
				btnUpdateStatus.setVisibility(View.GONE);
				txtUpdate.setVisibility(View.GONE);
				lblUpdate.setVisibility(View.GONE);
				lblUserName.setText("");
				lblUserName.setVisibility(View.GONE);

				btnLoginTwitter.setVisibility(View.VISIBLE);
			}
		});
		
		/** This if conditions is tested once is
		 * redirected from twitter page. Parse the uri to get oAuth
		 * Verifier
		 * */
		if (!TwitterUtil.isTwitterLoggedInAlready()) {
			uri = getIntent().getData();
			if (uri != null && uri.toString().startsWith(TwitterUtil.TWITTER_CALLBACK_URL)) {
				username = TwitterUtil.setoAuthVerf(uri);
				// Hide login button
				btnLoginTwitter.setVisibility(View.GONE);

				// Show Update Twitter
				lblUpdate.setVisibility(View.VISIBLE);
				txtUpdate.setVisibility(View.VISIBLE);
				btnUpdateStatus.setVisibility(View.VISIBLE);
				btnLogoutTwitter.setVisibility(View.VISIBLE);
				
				// Displaying in xml ui
				lblUserName.setText(Html.fromHtml("<b>Welcome " + username + "</b>"));
			}
		}
		else {
			btnLoginTwitter.setVisibility(View.GONE);
			
			btnLogoutTwitter.setVisibility(View.VISIBLE);
			btnUpdateStatus.setVisibility(View.VISIBLE);
			txtUpdate.setVisibility(View.VISIBLE);
			lblUpdate.setVisibility(View.VISIBLE);
		}
		
	}
	
	protected void onResume() {
		super.onResume();
	}

}
